<?php

namespace App\Service\Moment;

use App\Constants\ErrorCode;
use App\Model\MomentComment;
use App\Service\BaseService;

/**
 * 评论列表
 */
class MomentCommentListService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $momentId = $this->request->input('moment_id', 1);
        $page = $this->request->input('page', 1);
        $list = MomentComment::getList($momentId, $page);
        $ret = ['list' => []];
        array_walk($list, function ($item) use (&$ret, $userId) {
            $comment = MomentComment::buildItem($item, $userId);
            $commentId = $item->id;
            if (!isset($ret['list'][$commentId])) {
                $ret['list'][$commentId] = $comment;
            } else {
                $ret['list'][$commentId] = array_merge($comment, $ret['list'][$commentId]);
            }
        });
        //按一级留言倒序排列
        array_multisort(array_column($ret['list'], 'comment_id'), SORT_DESC, $ret['list']);
        $ret['list'] = array_values($ret['list']);
        $ret['total'] = count($ret['list']);
        // log_user_result($userId, $ret);
        return $ret;
    }
}
